<?php
declare (strict_types = 1);
/**
 * 管理员角色
 */
namespace app\common\model;

class AdminLog extends BaseModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 't_log';

    public function adminRoleHasOne()
    {
        return $this->hasOne(AdminRole::class, 'id', 'role_id');
    }
     /**
     * 日志列表
     */
    public function LogList($where = [], $whereRole = [], $offset = 1, $length = 20)
    {
        $field = ['a.id', 'a.user_id', 'a.role_id', 'a.ip', 'a.type', 'a.create_time', 'a.msg', 't.username'];

        $fieldRole = ['id', 'name'];

        $list = self::with([
            'adminRoleHasOne' => function ($query) use ($whereRole, $fieldRole) {
                $query->withField($fieldRole)->hidden(['id'])->where($whereRole);
            }
        ])->alias('a')
          ->field($field)
          ->where($where)
          ->limit(($offset-1) * $length, $length)
          ->join(['t_admin'=>'t'],'a.user_id=t.id')
          ->order(['id' => 'desc'])
          ->select();

        $count = self::where($where)->count('id');
        return [
            'log'  => $list,
            'total' => $count,
            'pagenum' => $offset
        ];
    }
}
